//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using CommunityServer.Galleries.Controls;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using Style = CommunityServer.Controls.Style;
using Content = CommunityServer.Components.Content;

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	/// Summary description for CommentEditor.
	/// </summary>
	public class CommentEditor : BaseGalleryPage
	{

		#region Child Controls

		protected Content TaskRegion;
		protected MPContainer MPContainer;
		protected PanelTabControl PanelTabControl1;
		protected LinkButton Save;
		protected TextBox ModeratedBy;
		protected TextBox Title;
		protected Editor Comment;
		protected TextBox Notes;
		protected TextBox OriginalContent;
		protected CheckBox Approved;
		protected ResourceControl FeedbackEditor_ModeratedBy;
		protected CheckBox PersonalComment;
		protected ResourceControl FeedbackEditor_Comment;
		protected ResourceControl FeedbackEditor_Notes;
		protected ResourceControl FeedbackEditor_OriginalComment;
		protected Style ControlPanelStyle;
		protected CA.TabStrip CommentEditorTabs;
		protected MPContent bcr;
		protected GalleryImage Galleryimage1;

		private GalleryPost currentPost = null;

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
			int postid = -1;

			
			Head.AddTitle(Components.ResourceManager.GetString("CP_Photos_CommentEdit_Title"), this.Context) ;

			if(Request.QueryString["pid"] != null)
			{
				try
				{
					postid = Int32.Parse(Request.QueryString["pid"]);
				}
				catch{}
			}

			if(postid > 0)
			{
				CSContext.Current.PostID = postid;
				currentPost = GalleryPosts.GetPicture(postid);

				// Update the thumbnail
				GalleryPost parentPost = GalleryPosts.GetPicture(currentPost.ParentID);
				Galleryimage1.GalleryPost = parentPost;
				Galleryimage1.PostID = parentPost.PostID;
				//Galleryimage1.NavigateUrl = GalleryUrls.Instance().ViewPicture(CurrentGallery.ApplicationKey, parentPost );
				Galleryimage1.DataBind();
			}
			else
			{
				currentPost = new GalleryPost() ;
			}

			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
				Save.Text = Components.ResourceManager.GetString( "FeedbackEditor_Save" );
				Comment.EnableHtmlModeEditing = true;
				//Personal Comments are not yet implimented in the gallery
				//if (PersonalComment != null)
				//	PersonalComment.Text = ResourceManager.GetString( "FeedbackEditor_PersonalComment" );

				if (Approved != null)
					Approved.Text = ResourceManager.GetString( "FeedbackEditor_Approved" );

				//Readonly in galleries
				Approved.Enabled = false;
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Save.Click += new EventHandler(this.Save_Click);
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		public override void DataBind() 
		{
			base.DataBind ();


			CSContext cntx = CSContext.Current;

			if(ModeratedBy != null)
				ModeratedBy.Text = cntx.User.DisplayName;
			
			if(Title != null)
				Title.Text = currentPost.Subject;

			if(Comment != null)
				Comment.Text = currentPost.Body;

			if(Notes != null)
				Notes.Text = currentPost.GetExtendedAttribute("Notes");

			//Personal Comments are not yet implimented in the gallery
			//if(PersonalComment != null)
			//	PersonalComment.Checked = (currentPost.GalleryPostType == GalleryPostType.PersonalComment);

			if(Approved != null)
				Approved.Checked = currentPost.IsApproved;

			if(OriginalContent != null)
			{
				// If this is the first time accessing the OriginalContent attribute, set it
				string origContent = currentPost.GetExtendedAttribute("OriginalContent");
				if (origContent == String.Empty)
				{
					origContent = currentPost.Body;
					cntx.Post = currentPost;
				}
            
				OriginalContent.Text = origContent;

			}

			// Stick the post in CSContext to it's available to the skin file
			cntx.Post = currentPost;
        
		}


		/// <summary>
		/// Provides a hook for custom logic in the skin file to easily save additional data to the post.
		/// </summary>
		protected void SaveAdditionalContent(GalleryPost post) 
		{

			// If the ExtendedAttribsToSave hashtable exists in HTPPContext,
			//  loop through each item and add them to the post's Extended Attributes
			Hashtable ExtendedAttribsToSave = CSContext.Current.Context.Items["ExtendedAttribsToSave"] as Hashtable;
			if(ExtendedAttribsToSave != null)
			{
				
				if (ExtendedAttribsToSave != null)
				{
					foreach (object key in ExtendedAttribsToSave.Keys)
					{
						post.SetExtendedAttribute(Convert.ToString(key), Convert.ToString(ExtendedAttribsToSave[key]));
					}
				}

			}
		}
	
	

		#region Private Methods

		private void SaveContent()
		{
			// Retrieve post from database to edit
			CSContext cntx = CSContext.Current; 
			currentPost = GalleryPosts.GetPicture(cntx.PostID);

			// De-Encode fields that will get re-HTML encoded when saved
			currentPost.Subject = Globals.HtmlDecode(currentPost.Subject);
			
			if (currentPost.HasSubmittedUserName)
				currentPost.SubmittedUserName = Globals.HtmlDecode(currentPost.SubmittedUserName);

			// Update post content
			if (Comment != null)
				currentPost.Body = Comment.Text;

			if (Title != null)
				currentPost.Subject = Title.Text;

			//Personal Comments are not yet implimented in the gallery
			//if (PersonalComment != null)
			//	currentPost.BlogPostType = PersonalComment.Checked ? BlogPostType.PersonalComment : BlogPostType.Comment;
                
			if (Notes != null)
				currentPost.SetExtendedAttribute("Notes", Notes.Text);

			if (OriginalContent != null)
				currentPost.SetExtendedAttribute("OriginalContent", OriginalContent.Text);

			//Readonly in galleries
			//if (Approved != null)
			//	currentPost.IsApproved = Approved.Checked;

			currentPost.PostType = PostContentType.HTML;
			currentPost.SetExtendedAttribute("ModeratedBy", cntx.User.Username);
             
			// Provide a hook for any child class to easily save stuff
			SaveAdditionalContent(currentPost);

			GalleryPosts.UpdatePicture(currentPost);

		}


		#endregion

		#region Button Click Handlers

		private void Save_Click(object sender, EventArgs e) 
		{

			if(!Page.IsValid)
				return;

			SaveContent();

			Modal.ClosePage(Page);
		}


		#endregion

	}
}
